<?php


namespace app\manage\model;


use think\Db;

class Advert
{

    /**
     * 首页banner列表
     */
    public function bannerList($page,$page_size){

        $data=Db::name('advert')
            ->field('id,advert_pic,status,sort,location')
            ->where(['type'=>1])
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('advert')
            ->where(['type'=>1])
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 首页banner 添加，编辑  location 1=顶部  2=底部 3=中部
     */
    public function bannerEdit($data){

        if(empty($data['id'])){
           /* if($data['location']==1){
                $advert['advert_top']=$data['advert_pic'];
            }else{
                $advert['advert_bottom']=$data['advert_pic'];
            }*/
            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['create_time']=time();
            $advert['type']=1;
            $advert['class_type']=10;
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->insert($advert);

        }else{
            $advert_id=$data['id'];

            /*if($data['location']==1){
                $advert['advert_top']=$data['advert_pic'];
            }else{
                $advert['advert_bottom']=$data['advert_pic'];
            }*/
            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['update_time']=time();
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->where(['id'=>$advert_id])->update($advert);

        }

        return true;
    }

    /**
     * banner 广告 删除  type 1=单个删除  2=批量删除
     *         status 1=首页  2=联盟商城 3=干货分享  4=返利  5=爆品广告  6=广告位
     */
    public function advertDel($id,$type,$status=1){
        if($type==1){
            Db::name('advert')
                ->where(['id'=>$id,'type'=>$status])
                ->delete();
        }else{
            Db::name('advert')
                ->where(['id'=>['in',$id],'type'=>$status])
                ->delete();
        }

        return true;
    }

    /**
     * banner 广告 详情  1=首页  2=联盟商城 3=干货分享  4=返利  5=爆品广告  6=广告位
     */
    public function advertDetail($id,$type=1,$field){
        $data=Db::name('advert')
            ->field($field)
            ->where(['type'=>$type,'id'=>$id])
            ->find();

        return $data;
    }

    /**
     * banner  广告  修改显示状态，排序
     *  status 1=首页  2=联盟商城 3=干货分享  4=返利  5=爆品广告  6=广告位
     *  goods_type 商品详情类型;1:消费爆品,2:返利商城,3:限时特惠,4:裂变爆品,5:商家讲坛
     */
    public function advertEdit($id,$data,$status=1){
        Db::name('advert')
            ->where(['type'=>$status,'id'=>$id])
            ->update($data);
        return true;
    }

    /**
     * 商城banner列表
     */
    public function mallList($page,$page_size){

        $data=Db::name('advert')
            ->field('id,advert_pic,status,sort,location')
            ->where(['type'=>2])
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('advert')
            ->where(['type'=>2])
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }


    /**
     * 商城banner 添加，编辑  location 1=顶部  2=底部 3=中部
     */
    public function mallEdit($data){

        if(empty($data['id'])){

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['create_time']=time();
            $advert['type']=2;
            $advert['class_type']=10;
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];
            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }
            Db::name('advert')->insert($advert);

        }else{
            $advert_id=$data['id'];

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['update_time']=time();
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];
            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }
            Db::name('advert')->where(['id'=>$advert_id])->update($advert);

        }

        return true;
    }


    /**
     * 返利广告列表
     */
    public function rebateList($page,$page_size){


        $data=Db::name('advert')
            ->field('id,advert_pic,status,sort,location')
            ->where(['type'=>4])
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('advert')
            ->where(['type'=>4])
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 返利广告 添加，编辑
     */
    public function rebateEdit($data){

        if(empty($data['id'])){

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['create_time']=time();
            $advert['type']=4;
            $advert['class_type']=10;
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->insert($advert);

        }else{
            $advert_id=$data['id'];

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['update_time']=time();
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->where(['id'=>$advert_id])->update($advert);

        }

        return true;
    }

    /**
     * 爆品商城 列表
     */
    public function explodeList($page,$page_size){


        $data=Db::name('advert')
            ->field('id,advert_pic,status,sort,location')
            ->where(['type'=>5])
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('advert')
            ->where(['type'=>5])
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 爆品   添加，编辑
     */
    public function explodeEdit($data){

        if(empty($data['id'])){

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['create_time']=time();
            $advert['type']=5;
            $advert['class_type']=10;
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->insert($advert);

        }else{
            $advert_id=$data['id'];

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['update_time']=time();
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->where(['id'=>$advert_id])->update($advert);

        }

        return true;
    }

    /**
     * 干活分享  列表
     */
    public function shareList($page,$page_size){


        $data=Db::name('advert')
            ->field('id,advert_pic,status,sort,location')
            ->where(['type'=>3])
            ->page($page,$page_size)
            ->select();

        $total_num=Db::name('advert')
            ->where(['type'=>3])
            ->count();
        $t_page=ceil($total_num / $page_size);

        $list['current_page'] = $page;
        $list['total_page'] =$t_page;
        $list['page_num'] = $page_size;
        $list['total_num'] = $total_num;
        $list['data'] = $data;

        return $list;
    }

    /**
     * 干活分享   添加，编辑
     */
    public function shareEdit($data){

        if(empty($data['id'])){

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['create_time']=time();
            $advert['type']=3;
            $advert['class_type']=10;
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->insert($advert);

        }else{
            $advert_id=$data['id'];

            $advert['advert_pic']=$data['advert_pic'];
            $advert['location']=$data['location'];
            $advert['status']=$data['status'];
            $advert['update_time']=time();
            $advert['sort']=$data['sort'];
            $advert['jump_type']=$data['jump_type'];
            $advert['jump_id']=$data['jump_id'];
            $advert['target_url']=$data['jump_url'];

            if($data['jump_type']==2){
                $advert['goods_type']=$data['goods_type'];
            }

            Db::name('advert')->where(['id'=>$advert_id])->update($advert);

        }

        return true;
    }



}